package com.yanqu.road.server.cmd.activity.operamask;

import com.yanqu.road.entity.activity.operamask.OperaMaskConfig;
import com.yanqu.road.entity.activity.operamask.OperaMaskSceneData;
import com.yanqu.road.entity.activity.operamask.OperaMaskUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.OperaMaskProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.operamask.OperaMaskModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.operamask.OperaMaskMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_OPERA_MASK_GET_SCENE_RANK_LIST, desc = "")
public class OperaMaskGetSceneRankListCmd extends AbstractCommand {

    private int sceneId;
    private int type = 0;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OperaMaskProto.OperaMaskGetSceneRankListReqMsg reqMsg = OperaMaskProto.OperaMaskGetSceneRankListReqMsg.parseFrom(packet.getBytes());
        sceneId = reqMsg.getSceneId();
        if (reqMsg.hasType()) {
            type = reqMsg.getType();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        OperaMaskConfig config = OperaMaskMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        long score = 0;
        int skillValue = 0;
        OperaMaskUserData userData = player.getModule(OperaMaskModule.class).getUserData();
        if (userData != null) {
            OperaMaskSceneData sceneData = userData.getSceneDataMap().get(sceneId);
            if (sceneData != null) {
                if (type == 0) {
                    score = sceneData.getScore();
                    skillValue = sceneData.getSkValue();
                } else {
                    score = sceneData.getScore2();
                    skillValue = sceneData.getSkValue2();
                }
            }
        }

        OperaMaskProto.CrossOperaMaskGetSceneRankListReqMsg.Builder builder = OperaMaskProto.CrossOperaMaskGetSceneRankListReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setSceneId(sceneId);
        builder.setScore(score);
        builder.setType(type);
        builder.setSkillValue(skillValue);
        player.sendPacket(CrossProtocol.C2_OPERA_MASK_GET_SCENE_RANK_LIST, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
        OperaMaskProto.OperaMaskGetSceneRankListRespMsg.Builder builder = OperaMaskProto.OperaMaskGetSceneRankListRespMsg.newBuilder();
        builder.setRet(ret);
        player.sendPacket(ClientProtocol.U_OPERA_MASK_GET_TIPS, builder);
    }
}
